package org.csu.code.api.controller;

import org.csu.code.api.common.CommonResponse;
import org.csu.code.api.entity.Account;
import org.csu.code.api.entity.Profile;
import org.csu.code.api.entity.Signon;
import org.csu.code.api.service.AccountService;
import org.csu.code.api.service.RedisService;
import org.csu.code.api.vo.AccountVO;
import org.csu.code.api.vo.SignonVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/account/")
public class AccountController {

    @Autowired
    private AccountService accountService;

    //登录
    @PostMapping("login")
    @ResponseBody
    public CommonResponse<AccountVO> login(@RequestBody Signon signon) {

        return accountService.login(signon);
    }

    @GetMapping("users")
    @ResponseBody
    public CommonResponse<List<AccountVO>> getAccountList() {
        return accountService.getAccountList();
    }

    @GetMapping("user/{id}")
    @ResponseBody
    public CommonResponse<AccountVO> getAccountByUserId(@PathVariable("id") int userId) {
        return accountService.getAccountByUserId(userId);
    }

    @GetMapping("user/phone/{phone}")
    @ResponseBody
    public CommonResponse<AccountVO> getAccountByPhone(@PathVariable("phone") String phone) {
        return accountService.getAccountByPhone(phone);
    }

    //查询profile
    @GetMapping("profiles")
    @ResponseBody
    public CommonResponse<List<Profile>> getallAccountprofile() {
        return accountService.getallAccountprofile();
    }

    //添加用户
    @PostMapping("insert")
    @ResponseBody
    public CommonResponse<String> insertAccount(@RequestBody AccountVO accountVO) {
        return accountService.insertAccount(accountVO);
    }

    //更新信息
    @PutMapping("update")
    @ResponseBody
    public CommonResponse<String> updateAccount(@RequestBody AccountVO accountVO, HttpServletRequest request){
        return accountService.updateAccount(accountVO,request);
    }

}